package com.yzq.common;

import com.yzq.enums.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应类
 */
@Data
public class Result<T> implements Serializable {
    private int code;
    private String message;
    private T data;

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("操作成功");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> error(int errorCode, String message) {
        Result<T> result = new Result<>();
        result.setCode(errorCode);
        result.setMessage(message);
        return result;
    }


    public static <T> Result<T> error(ErrorCode errorCode) {
        Result<T> result = new Result<>();
        result.setCode(errorCode.getCode());
        result.setMessage(errorCode.getMessage());
        return result;
    }
}
